package com.soft.mainsite.module.staticstrategy.web;

import cn.hutool.core.util.StrUtil;
import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.common.exception.SystemException;
import com.soft.mainsite.common.template.TemplateFile;
import com.soft.mainsite.common.template.TemplateFileService;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.staticstrategy.entity.StaticStrategy;
import com.soft.mainsite.module.staticstrategy.entity.TemplateManage;
import com.soft.mainsite.module.staticstrategy.service.TemplateManagerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
import java.util.List;

/**
 * 对网站页面静态化控制类
 *
 * @author liupengtao
 * @create 2019-04-29
 **/

@Controller
@RequestMapping("/system/staticHtml")
public class StaticHtmlController {

	@Autowired
	private TemplateFileService templateFileService;

	@Autowired
	private TemplateManagerService templateManagerService;

	@Value("${system.site.static.path}")
	String staticPath;

	/**
	 * 静态化页面
	 * @return
	 */
	@RequestMapping("/static")
	public String siteContentStatic(Model model) {
		List<TemplateFile> list = templateFileService.findAll();
		model.addAttribute("templateFiles",list);
		return "staticHtml/static";
	}

	@RequiresPermissions("template:edit")
	@RequestMapping("/input")
	public String input(TemplateFile templateFile, Model model){

		if(templateFile.getFilePath() == null){
			throw new SystemException("模板路径不能为空！");
		}

		//根据文件路径查询模板文件
		TemplateFile byPath = templateFileService.findByPath(templateFile.getFilePath());

		String tplPath = byPath.getFilePath();
		boolean isMatch = tplPath.contains(File.separator + CmsConst.WEB_PATH  + File.separator);

		String tplName = "";

		if(isMatch){
			//web端模板
			tplName = StrUtil.subAfter(tplPath, File.separator + CmsConst.WEB_PATH + File.separator,true);
		} else {
			//移动端模板
			tplName = StrUtil.subAfter(tplPath, File.separator + CmsConst.TPL_TYPE_MOBILE + File.separator,true);
		}

		tplName = tplName.replaceAll("\\\\", "/").replace(".html","");

		TemplateManage templateManage = templateManagerService.querfyTemplateManageByName(tplName);
		List<TemplateManage> templateRelationManageList = templateManagerService.queryRelationManageByParnetTemplate(tplName);
		List<TemplateManage> templateTriggerManageList = templateManagerService.queryTriggerManageByParnetTemplate(tplName);

		model.addAttribute("templateManage", templateManage);
		model.addAttribute("tplName", tplName);
		model.addAttribute("templateTriggerManageList", templateTriggerManageList);
		model.addAttribute("templateRelationManageList", templateRelationManageList);

		return "staticHtml/static_input";
	}

	@ResponseBody
	@RequestMapping("/saveStatic")
	public String saveStatic(StaticStrategy staticStrategy){
		templateManagerService.saveTemplateMange(staticStrategy);
		return JsonUtil.toSuccess("添加成功", "static-html-tab", false);
	}

}
